//
//  WYTCityItemVC.m
//  WuyouTang
//
//  Created by yunbao02 on 2024/8/28.
//  Copyright © 2024 cat. All rights reserved.
//

#import "WYTCityItemVC.h"

#import "WYTCityItemCell.h"
#import "WYTVideoCell.h"
#import "YBGetVideoObj.h"
#import "NearbyVideoModel.h"
#import "ShopDetailVC.h"
#import "WSLWaterFlowLayout.h"

@interface WYTCityItemVC ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,WSLWaterFlowLayoutDelegate>
{
    BOOL _oldShopListState;
    int _pageShop;
    NSString *_shopUrl;
    int _pageVideo;
    NSString *_videoUrl;
    BOOL _curShow;
}
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataShops;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *dataVideos;
@end

@implementation WYTCityItemVC

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    _curShow = NO;
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _curShow = YES;
    NSLog(@"===>shoplist:viewWillAppear==>:%d===>:old:%d",[PublicObj isShopList],_oldShopListState);
    NSLog(@"===>view:%@",_classID);
    if (_oldShopListState != [PublicObj isShopList] || _dataShops.count<=0 || _dataVideos.count<=0) {
        _oldShopListState = [PublicObj isShopList];
        // 刷新列表
        [self pulldata];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.naviView.hidden = YES;
    _oldShopListState = [PublicObj isShopList];
    //NSLog(@"===>shoplist:didload==>:%d",[PublicObj isShopList]);
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeUI) name:wytChangeCityUi object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLocCity:) name:wytChangeLocCity object:nil];
    _dataShops = [NSMutableArray array];
    _pageShop = 1;
    _dataVideos = [NSMutableArray array];
    _pageVideo = 1;
    
    _videoUrl = @"Video.getNearby";
    _shopUrl = @"Shop.getNearby";
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.collectionView];
    _tableView.hidden = ![PublicObj isShopList];
    _collectionView.hidden = [PublicObj isShopList];
}
-(void)changeLocCity:(NSNotification *)noti {
    NSLog(@"===>shoplist[通知]");
   if (_curShow) {
       NSLog(@"===>shoplist[通知-执行]");
       _pageShop = 1;
       _pageVideo  = 1;
       _curCity = [NSString stringWithFormat:@"%@", noti.object];
       [self pulldata];
   }
}
-(void)changeUI {
     NSLog(@"===>shoplist[通知]");
    if (_curShow) {
        _oldShopListState = [PublicObj isShopList];
        NSLog(@"===>shoplist[通知-执行]");
        [self pulldata];
    }
}
-(void)pulldata {
    if ([PublicObj isShopList]) {
        _tableView.hidden = NO;
        _collectionView.hidden = YES;
        [self pullShopList];
    }else {
        [self pullVideoList];
        _tableView.hidden = YES;
        _collectionView.hidden = NO;
    }
    NSLog(@"===>shoplist====[finishi]:%@",_curCity);
}

#pragma mark - 商家
-(void)pullShopList {
    NSDictionary *postDic = @{
        @"lng":[CityDefault getLocationLng],
        @"lat":[CityDefault getLocationLat],
        @"p":@(_pageShop),
        @"city":_curCity?_curCity:@"",
        @"cityclass_id":_classID,
    };
    _tableView.userInteractionEnabled = NO;
    YBWeakSelf;
    [WYNetworking postWithUrl:_shopUrl Dic:postDic Suc:^(int code, id info, NSString *msg) {
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        _tableView.userInteractionEnabled = YES;
        if (code == 0) {
            NSArray *infoA = [NSArray arrayWithArray:info];
            if (_pageShop == 1) {
                [_dataShops removeAllObjects];
            }
            if (infoA.count <= 0) {
                [_tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [_dataShops addObjectsFromArray:infoA];
            }
            if (_dataShops.count > 0) {
                [PublicView hiddenTextNoData:_tableView];
            }else{
                [weakSelf showShopNoDataTips];
            }
            [_tableView reloadData];
            
        }else{
            if (_dataShops) {
                [_dataShops removeAllObjects];
            }
            [_tableView reloadData];
            [weakSelf showShopNoDataTips];
        }
        
    } Fail:^(id fail) {
        _tableView.userInteractionEnabled = YES;
        if (_dataShops) {
            [_dataShops removeAllObjects];
        }
        [_tableView reloadData];
        [weakSelf showShopNoDataTips];
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
    }];
}
-(void)showShopNoDataTips {
    if ([_classID isEqual:@"-1"]) {
        [PublicView showTextNoData:_tableView text1:YZMsg(@"你还没有关注任何人") text2:YZMsg(@"赶快去关注自己感兴趣的人吧") centerY:0.8];
    }else {
        [PublicView showTextNoData:_tableView text1:@"" text2:YZMsg(@"暂无商家哦~") centerY:0.8];
    }
}
#pragma mark - UITableViewDelegate、UITableViewDataSource
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataShops.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WYTCityItemCell *cell = [WYTCityItemCell cellWithTab:tableView index:indexPath];
    cell.dataDic = _dataShops[indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *subDic = _dataShops[indexPath.row];
    ShopDetailVC *vc = [[ShopDetailVC alloc]init];
    vc.toUserID = strFormat([subDic valueForKey:@"uid"]);
    [[YBBaseAppDelegate sharedAppDelegate] pushViewController:vc animated:YES];
}

#pragma mark - set/get
-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,64+statusbarHeight, _window_width, _window_height-64-statusbarHeight-ShowDiff-49)style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = UIColor.clearColor;
        YBWeakSelf;
        MJRefreshNormalHeader *mjheader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            _pageShop = 1;
            [weakSelf pulldata];
        }];
        _tableView.mj_header = mjheader;
        MJRefreshBackNormalFooter *mjfooter = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            _pageShop += 1;
            [weakSelf pulldata];
        }];
        _tableView.mj_footer = mjfooter;
    }
    return _tableView;
}

#pragma mark - 视频
-(void)pullVideoList {
    _collectionView.userInteractionEnabled = NO;
    NSDictionary *postDic = @{
        @"lng":[CityDefault getLocationLng],
        @"lat":[CityDefault getLocationLat],
        @"p":@(_pageVideo),
        @"city":_curCity?_curCity:@"",
        @"cityclass_id":_classID,
    };
    YBWeakSelf;
    [WYNetworking postWithUrl:_videoUrl Dic:postDic Suc:^(int code, id info, NSString *msg) {
        [_collectionView.mj_header endRefreshing];
        [_collectionView.mj_footer endRefreshing];
        _collectionView.userInteractionEnabled = YES;
        if (code == 0) {
            NSArray *infoA = [NSArray arrayWithArray:info];
            if (_pageVideo == 1) {
                [_dataVideos removeAllObjects];
            }
            if (infoA.count <= 0) {
                [_collectionView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [_dataVideos addObjectsFromArray:infoA];
            }
            if (_dataVideos.count > 0) {
                [PublicView hiddenTextNoData:_collectionView];
            }else{
                [weakSelf showVideoNoDataTips];
            }
            [_collectionView reloadData];
            
        }else{
            if (_dataVideos) {
                [_dataVideos removeAllObjects];
            }
            [_collectionView reloadData];
            [weakSelf showVideoNoDataTips];
        }
    } Fail:^(id fail) {
        _collectionView.userInteractionEnabled = YES;
        if (_dataVideos) {
            [_dataVideos removeAllObjects];
        }
        [_collectionView reloadData];
        [weakSelf showVideoNoDataTips];
        [_collectionView.mj_header endRefreshing];
        [_collectionView.mj_footer endRefreshing];
    }];
    
}
-(void)showVideoNoDataTips {
    if ([_classID isEqual:@"-1"]) {
        [PublicView showTextNoData:_collectionView text1:YZMsg(@"你还没有关注任何人") text2:YZMsg(@"赶快去关注自己感兴趣的人吧") centerY:0.8];
    }else {
        [PublicView showTextNoData:_collectionView text1:@"" text2:YZMsg(@"暂无视频哦~") centerY:0.8];
    }
}
#pragma mark - CollectionView 代理

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0,12,0,12);
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataVideos.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    WYTVideoCell *cell = (WYTVideoCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"WYTVideoCell" forIndexPath:indexPath];
    cell.dataDic = _dataVideos[indexPath.row];;
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *vidoeID = minstr([_dataVideos[indexPath.row] valueForKey:@"id"]);
    [YBGetVideoObj lookManeger].fromWhere = @"WuyouTangClassVC";
    [YBGetVideoObj lookManeger].videoID = vidoeID;
    [YBGetVideoObj lookManeger].playIndex = (int)indexPath.row;
    [YBGetVideoObj lookManeger].videoList = [_dataVideos mutableCopy];
    [YBGetVideoObj lookManeger].paging = (int)_pageVideo;
    [YBGetVideoObj lookManeger].baseUrl = _videoUrl;
    [[YBGetVideoObj lookManeger]goLookVC];
}

- (CGSize)waterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat f = arc4random_uniform(40);
    CGFloat itemW = (_window_width - 37)/2;
    CGFloat itemH = itemW *184.0/170 + 68 + f;
    return CGSizeMake(itemW, itemH);
}

/** 列数*/
-(CGFloat)columnCountInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout {
    return  2;
}
/** 列间距*/
-(CGFloat)columnMarginInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout {
    return 12;
}
/** 行间距*/
-(CGFloat)rowMarginInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout {
    return 12;
}

//flow.minimumLineSpacing = 12;
//flow.minimumInteritemSpacing = 12;

#pragma mark - set/get
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        WSLWaterFlowLayout *flow = [[WSLWaterFlowLayout alloc] init];
        flow.delegate = self;
        flow.flowLayoutStyle = WSLWaterFlowVerticalEqualWidth;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,64+statusbarHeight, _window_width, _window_height-64-statusbarHeight-ShowDiff-49) collectionViewLayout:flow];
        [_collectionView registerNib:[UINib nibWithNibName:@"WYTVideoCell" bundle:nil] forCellWithReuseIdentifier:@"WYTVideoCell"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = Normal_Color;
        YBWeakSelf;
        MJRefreshNormalHeader *mjheader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            _pageVideo = 1;
            [weakSelf pulldata];
        }];
        _collectionView.mj_header = mjheader;
        MJRefreshBackNormalFooter *mjfooter = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            _pageVideo += 1;
            [weakSelf pulldata];
        }];
        _collectionView.mj_footer = mjfooter;
        
    }
    return _collectionView;
}







@end
